from django.conf import settings
from fdfs_client.client import Fdfs_client
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from goods.models import SKUImage, SKU
from meiduo_admin.serializers.images import ImageSerializer, SKUSerializer
from meiduo_admin.utils import PageNum
from rest_framework.permissions import IsAdminUser


class ImageView(ModelViewSet):
    """
    图片增删改查
    """
    # 指定查询集
    queryset = SKUImage.objects.all()

    # 指定序列化器
    serializer_class = ImageSerializer
    # 指定分页器
    pagination_class = PageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    def simple(self, request):
        """
        获取sku_id数据
        :param request:
        :return:

        """
        # 1.获取所有sku数据
        skus = SKU.objects.all()

        # 2.序列化sku数据
        ser = SKUSerializer(skus, many=True)

        return Response(ser.data)

    def create(self, request, *args, **kwargs):
        # 1.获取前端数据
        data = request.data
        # 获取更新对象
        image=self.get_object()
        # ２．验证数据
        ser = self.get_serializer(image,data=data)
        ser.is_valid(raise_exception=True)

        ser.save()
        return Response(ser.data)

